package com.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.dao.BookDao;
import com.stu.entity.Book;
import com.stu.service.Bookservice;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wzm
 * @create 2023-03-01 10:32
 */
//业务逻辑的的实现类 继承了接口的规范 同时需要调用dao的方法来执行操作
@Service
public class BookServiceImpl implements Bookservice {
    @Autowired
    private BookDao bookDao;

    @Override
    public Boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    @Override
    public Boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }

    @Override
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    public List<Book> getAll() {
        return bookDao.selectList(null);
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        return bookDao.selectPage(page,null);
    }

    @Override
    public IPage<Book> getPage1(int currentPage, int pageSize, Book book) {
        IPage page = new Page(currentPage,pageSize);
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        //判断 有条件+条件  没有条件不加   type  name  description
        //如果搜索框中有type
        //where type like ？
        lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());
        //如果搜索框中有name
        lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());

        //如果搜索框中有dedcription
        lqw.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription());

        //用条件查询分页 但是 可能条件有或没有
        return bookDao.selectPage(page,lqw);
    }
}
